package com.hbter630.change.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbter630.change.DTO.CartproductDTO;
import com.hbter630.change.entity.Cartproduct;
import com.hbter630.change.entity.Order_;
import com.hbter630.change.entity.Product;
import com.hbter630.change.mapper.CartproductMapper;
import com.hbter630.change.service.CartproductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbter630.change.service.OrderService;
import com.hbter630.change.service.ProductService;
import com.hbter630.change.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-05-24
 */
@Service
@Slf4j
public class CartproductServiceImpl extends ServiceImpl<CartproductMapper, Cartproduct> implements CartproductService {

    @Autowired
    private ProductService productService;//物品

    @Resource
    private OrderService orderService;


    @Override
    public Result getListById() {

        String userId = StpUtil.getLoginId().toString();

        QueryWrapper<Cartproduct> cartproductQueryWrapper = new QueryWrapper<>();
        cartproductQueryWrapper.eq("user_id",Integer.parseInt(userId));
        //得到当前用户的购物车 物品
        List<Cartproduct> list = this.list(cartproductQueryWrapper);

        ArrayList<CartproductDTO> cartproductDTOS = new ArrayList<>();
        for( Cartproduct one :list){

            Product byId = productService.getById(one.getProductId());
            CartproductDTO cartproductDTO = new CartproductDTO();
            cartproductDTO.setPrice(byId.getPrice());
            cartproductDTO.setCategoryName(byId.getName());
            cartproductDTO.setQuantity(one.getQuantity());
            cartproductDTO.setImageUrl(byId.getImageUrl());
            cartproductDTO.setCartProductId(one.getCartProductId());
            //加入DTO
            cartproductDTOS.add(cartproductDTO);

        }
        return Result.ok().addData(cartproductDTOS);
    }

    @Override
    public Result addOrderTable(List<CartproductDTO> cartproductDTOS) {

        log.info("========将购物车列表 加入订单 ========{}",cartproductDTOS.toString());
        if(cartproductDTOS!=null){
            //先删除购物车 在添加进订单表
            int loginIdAsInt = StpUtil.getLoginIdAsInt();
            QueryWrapper<Cartproduct> QueryWrapper = new QueryWrapper<>();
            QueryWrapper.eq("user_id",loginIdAsInt);
            this.remove(QueryWrapper);

            //添加进购物车列表
            Order_ order = new Order_();
            //设置订单id
            String sID = IdUtil.simpleUUID();
            order.setOrderId(sID);//订单id
            order.setUserId(loginIdAsInt);//用户id
            order.setStatus(1);//订单状态
            String o_name="";
            double price=0;
            for (CartproductDTO a: cartproductDTOS) {
                //添加商品名字

                String price1 = a.getPrice().toString();
                Integer quantity = a.getQuantity();
                o_name+=a.getCategoryName()+"*"+quantity+";";
                double parseInt = Double.parseDouble(price1);
                price+=parseInt*quantity;
            }
            order.setNames(o_name);
            //总价钱
            order.setTotalAmount(price);
//            orderService.save(order);

            log.info("当前order的值：{}",order);

            orderService.save(order);



            return Result.ok().addData(Result.ok());
        }
        return Result.failed();
    }
}
